<?php
require_once "model.php";
use addons\weidun_card\model\model;

/**
 * Class tips
 */
class user extends model
{
    public $table = 'mc_mapping_fans';

    /**
     * 统计
     * @return array
     */
    public function statistic()
    {
        global $_W, $_GPC;
        $return = array();
        $return['all'] = array();
        $params = array(':uniacid' => $_W['uniacid']);
        $where = "";
        $p = trim($_GPC['start_time']);
        if (!empty($p)) {
            $where .= " AND followtime >= :start_time";
            $params[':start_time'] = strtotime($p);
        }
        unset($p);
        $p = trim($_GPC['end_time']);
        if (!empty($p)) {
            $where .= " AND followtime <= :end_time";
            $params[':end_time'] = strtotime($p);
        }
        unset($p);

        // 用户总数
        $sql = "SELECT COUNT(fanid) FROM " . tablename($this->table) . " WHERE uniacid = :uniacid {$where}";
        $return['all']['sum'] = pdo_fetchcolumn($sql, $params);
        if (empty($return['all']['sum'])) $return['all']['sum'] = 0;

        $start_time = strtotime(date('Y-m-d', time()));
        $end_time = time();

        $return['day'] = array();
        // 今日新增
        $params[':start_time'] = $start_time;
        $params[':end_time'] = $end_time;
        $sql = "SELECT COUNT(fanid) FROM " . tablename($this->table) . " WHERE uniacid = :uniacid AND `followtime` >=:start_time AND `followtime` <=:end_time {$where}";
        $return['day']['sum'] = pdo_fetchcolumn($sql, $params);
        if (empty($return['day']['sum'])) {
            $return['day']['sum'] = 0;
        }

        // 本周新增
        $start_time = strtotime(date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"))));
        $end_time = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"))));
        $return['week'] = array();
        $params[':start_time'] = $start_time;
        $params[':end_time'] = $end_time;
        $sql = "SELECT COUNT(fanid) FROM " . tablename($this->table) . " WHERE uniacid = :uniacid AND `followtime` >:start_time AND `followtime` < :end_time {$where}";
        $return['week']['sum'] = pdo_fetchcolumn($sql, $params);
        if (empty($return['week']['sum'])) {
            $return['week']['sum'] = 0;
        }

        // 月新增
        $start_time = strtotime(date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y"))));
        $end_time = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y"))));
        $return['month'] = array();
        $params[':start_time'] = $start_time;
        $params[':end_time'] = $end_time;
        $sql = "SELECT COUNT(fanid) FROM " . tablename($this->table) . " WHERE uniacid = :uniacid AND followtime >=:start_time AND followtime <=:end_time {$where}";
        $return['month']['sum'] = pdo_fetchcolumn($sql, $params);
        if (empty($return['month']['sum'])) {
            $return['month']['sum'] = 0;
        }

        return $return;
    }
}